﻿Imports System.IO
Imports System.Data.SqlClient

Module Module1
    Public ConnectionString As String = "Data Source={SQLserver};" & _
                                                "Failover Partner=VMSQDARA001;" & _
                                                "Initial Catalog=isuzu;" & _
                                                "Integrated Security=false;" & _
                                                "User ID=tpt;" & _
                                                "Password=isuzu,123"
    Public Const TASK_WORK As String = "UpdateWorkingHour"
    Public Const TASK_SCHE As String = "UpdateSchedule"
    Public Const TASK_ASK_SCHE As String = "AskSchedule"
    Public Const TASK_ASK_ONL As String = "AskOnline"
    Public Const TASK_IDLE As String = "Idling"

    Public Const SETTING_FILE As String = "HostSetting.ini"

    Public nShops As Integer
    Public Shops(10) As String
    Public ShopsIP(10) As String
    Public ShopsPort(10) As Integer

    Public RetryEverySec As Integer
    Public nRetries As Integer
    Public CheckConnEveryMin As Integer
    Public CurrentTask As String = ""
    Public DebugMode As Integer
    Public ShowHiddenSheets As Boolean

    Public NeedToUpdateWorkHour As Boolean
    Public WorkingHourString As String

    Public Function ConvertMonth2Int(m As String) As Integer
        m = m.ToUpper.Trim

        Select Case m
            Case "JAN" : Return 1
            Case "FEB" : Return 2
            Case "MAR" : Return 3
            Case "APR" : Return 4
            Case "MAY" : Return 5
            Case "JUN" : Return 6
            Case "JUL" : Return 7
            Case "AUG" : Return 8
            Case "SEP" : Return 9
            Case "OCT" : Return 10
            Case "NOV" : Return 11
            Case "DEC" : Return 12
        End Select
        Return 0
    End Function

    Public Sub ReadProdFile(shopCode As String, ip As String)

        Dim fR As StreamReader
        Dim st As String
        Dim stArr() As String
        Dim fn As String

        Try
            fn = "\\" & ip & "\isuzu\production.txt"
            'fn = "production.txt"

            If File.Exists(fn) Then
                'delete all in DB where date = this date
                Using connection As New SqlConnection(ConnectionString)
                    connection.Open()

                    'delete all in the db with the same month
                    Dim sql = String.Format("DELETE FROM Production WHERE StepId='{0}' AND EndDate='{1}'", _
                                            shopCode, Today.ToString("yyyy-MM-dd"))

                    Dim cmd As New SqlCommand(sql, connection)
                    cmd.CommandType = CommandType.Text
                    cmd.ExecuteNonQuery()


                    fR = New StreamReader(fn)
                    fR.ReadLine() 'skip this 1st line

                    While fR.Peek <> -1
                        st = fR.ReadLine
                        stArr = st.Split("|")

                        sql = String.Format("INSERT INTO Production(StepId, TruckId, EndDate, EndTime, Duration) VALUES ('{0}', '{1}', '{2}', '{3}', {4})", _
                                            shopCode, stArr(0), Today.ToString("yyyy-MM-dd"), stArr(1), stArr(2))
                        cmd = New SqlCommand(sql, connection)
                        cmd.ExecuteNonQuery()
                    End While

                    fR.Close()
                End Using
            Else
                If DebugMode = 1 Then
                    MessageBox.Show("File production.txt doesn't exist")
                End If

            End If

        Catch ex As Exception
            If DebugMode Then
                MessageBox.Show(ex.ToString)
            End If
        End Try

    End Sub

End Module

Public Class message2Send
    Public msg As String
    Public code As String
    Public toIP As String
    Public toPort As Integer
    Public nRetries As Integer
    Public shopOrder As Integer

    Public Sub New(msg As String, code As String, toIP As String, toPort As Integer, shopOrder As Integer)
        Me.msg = msg
        Me.code = code
        Me.toIP = toIP
        Me.toPort = toPort
        Me.shopOrder = shopOrder
        Me.nRetries = 0
    End Sub
End Class

Public Class schedule
    Public myDate As Date
    Public plan As String

    Public Sub New(dd As Date, plan As String)
        Me.myDate = dd
        Me.plan = plan
    End Sub
End Class
